﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;

namespace Banco
{
    public enum ConfigurationType
    {
        DB,
        AppSettings
    }

    public enum DataSource
    {
        CIRS
    }

    [Serializable]
    public class ConfiguradorConexao
    {
        public ConfiguradorConexao(string urlBd)
        {
            _UrlBD = urlBd;
        }

        private string _UrlBD { get; set; }

        public SqlConnection Conn
        {
            get { return new SqlConnection(_UrlBD); }
        }

        private static IDictionary<DataSource, ConfiguradorConexao> _connectionsConfigurations;

        public static string App_Path
        {
            get { return Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) + @"\"; }
        }

        public static IDictionary<DataSource, ConfiguradorConexao> ConfiguracaoConexao
        {
            get
            {
                if (_connectionsConfigurations == null)
                {
                    LoadConfiguration();
                }

                return _connectionsConfigurations;
            }
        }

        public static ConfiguradorConexao ConexaoBD(DataSource dataSource)
        {
            if (_connectionsConfigurations == null)
            {
                LoadConfiguration();
            }

            return _connectionsConfigurations[dataSource];
        }

        private static ConfiguradorConexao GetConnectionConfigurator(string urlBD)
        {
            var result = new ConfiguradorConexao(urlBD);
            return result;
        }

        private static void LoadConfiguration()
        {
            if (_connectionsConfigurations == null)
            {
                if (_connectionsConfigurations == null)
                {
                    _connectionsConfigurations = new Dictionary<DataSource, ConfiguradorConexao>();
                }

                const string creanet = @"Data Source=ARCHMED\SQLEXPRESS;Initial Catalog=CIRS;User=@user;Password=@password;MultipleActiveResultSets=true;Pooling=true;Min Pool Size=3;Max Pool Size=200;";
                string resultado = creanet.Replace("@user", "elliackin").Replace("@password", "123");
                _connectionsConfigurations.Add(DataSource.CIRS, GetConnectionConfigurator(resultado));
            }
        }
    }
}